﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NXDO.Data.Extension;
using NXDO.Data.Partition;
using NXDO.WebAPI.Controllers;
using System;
using System.IO;
using TDM.DbData.DataEntity;
using TDM.WebBase;
using TDM.WebBiz.TestData;

namespace TDM.WebSvr.Controllers.TestData.File
{
    [AllowAnonymous]
    public class DataImportUpDnldController : ApiBinaryController
    {
        protected override string GetSaveFileName(string name, bool nameIsMimeContentType)
        {
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user?.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            var basePath = BasePathHelper.GetDataImportPath();
            long id = DbGenerateSnowKey.GetSnowKey(1, 1);
            var filePath = Path.Combine(DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), id.ToString() + ".gdmspack");
            DataImport data = new()
            {
                ImportTime = DateTime.Now,
                FileName = name,
                IsImport = false,
                FilePath = filePath,
                ImportID = id,
            };
            data.DBLinq().ToCommand(data).Insert();
            return Path.Combine(basePath, filePath);
        }

        public override object Upload([FromRoute] string name)
        {
            return base.Upload(name);
        }

        public override object Download([FromRoute] string name)
        {

            return base.Download(name);
        }

    }
}
